#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from Ui_dialog_add_new_relation import *
#from EA import *

class dialog_add_New_relation(QtGui.QDialog, Ui_Dialog_add_new_relation):
    
    
    def __init__(self,parent,AE_parent, relation_type_id, ontology):
        super(dialog_add_New_relation,self).__init__(parent)
        
        self.setupUi(self)
        self.parent=parent
        self.AE_parent = AE_parent
        self.relation_type_id = relation_type_id
        self.ontology = ontology
        
        # Set parent and relation pre-selected
        self.lineEdit_parent.setText( QtCore.QString.fromUtf8(self.AE_parent.name))
        
        self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),self, QtCore.SLOT("accept()"))
        self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"),self, QtCore.SLOT("reject()"))
        
        # La liste des types de relations dans la comboBox.
        self.comboBox_relations.clear() 
        self.liste_relations = self.ontology.get_all_relation_types()
        self.vector_relation_type = self.ontology.get_relation(self.relation_type_id)
        
         # liste_relations == all the relations where self.AE appear.
         # liste_relations = self.ontology.get_all_relation_selected(self.AE.AE_id)
        
        
        i = 0
        
        for relation in self.liste_relations:
            self.comboBox_relations.addItem(QtCore.QString.fromUtf8(relation.name))
            # now, we search the index of the current type_relation
            if (relation.name == self.vector_relation_type.name):
                n = i
            i += 1
        
        # Set the current relation position's in the comboBox
        self.comboBox_relations.setCurrentIndex(n)
       
        # Set the list of AE in the combobox
        self.comboBoxchild.clear()
        self.all_AEs = self.ontology.get_all_AEs()  
        self.comboBoxchild.addItem ( QtCore.QString.fromUtf8("Select child..."))
        for AE in self.all_AEs:
            self.comboBoxchild.addItem ( QtCore.QString.fromUtf8(AE.name))
        
    
    def accept(self):
        
        self.boolean = (self.comboBoxchild.currentText() != "Select child...")
        
        if self.boolean:
        
            self.selected_parent = self.lineEdit_parent.text()
            
            # "-1" because the first case of the comboBox is use by "select child..."
            self.vector_selected_child = self.all_AEs[self.comboBoxchild.currentIndex()-1]
            self.vector_selected_relation = self.liste_relations[self.comboBox_relations.currentIndex()]
            self.vector_selected_parent = [self.AE_parent.AE_id, self.AE_parent.name, self.AE_parent.definition]
            
            QtGui.QDialog.accept(self)
        else:
            QtGui.QMessageBox.critical ( self.parent, "warning", "Child is not selected.") 
          
        return